package com.qf.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Component
public class UserUtils {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    JwtUtils jwtUtils;

    public Integer getUserId(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if (cookies!=null&&cookies.length>0){
            String token=null;
            for (Cookie cook:cookies
            ) {
                if (cook.getName().equals("token")){
                    token=cook.getValue();
                }
            }
            //从redis中获取到jwt的加密信息
            Boolean aBoolean = redisTemplate.hasKey(token);
            if (aBoolean){
                Object o = redisTemplate.opsForValue().get(token);
                //进行解密
                Integer id = jwtUtils.verfiy(o.toString());
                return id;
            }
        }
        return 0;
    }
}
